package boot.spring.service.impl;



import boot.spring.service.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import boot.spring.mapper.LoginMapper;
import boot.spring.po.Staff;
import boot.spring.service.LoginService;

@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,timeout=5)
@Service("loginservice")
public class LoginServiceImpl implements LoginService{
	@Autowired
	LoginMapper loginmapper;
	@Autowired
	WebSocketServer webSocketRealUserServer;

	public String getpwdbyname(String name) {
		Staff s=loginmapper.getpwdbyname(name);
		if(s!=null)
		return s.getPassword();
		else
		return null;
	}
	
	public Long getUidbyname(String name) {
		Staff s=loginmapper.getpwdbyname(name);
		if(s!=null)
			return (long) s.getStaff_id();
			else
			return null;
	}
	
	public String getnamebyid(long id) {
		Staff s=loginmapper.getnamebyid(id);
		if(s!=null)
			return s.getUsername();
			else
			return null;
	}

    @Override
    public Staff updateStaff(Staff staff) {
		loginmapper.updateStaff(staff);
		//通知websocket服务器有用户信息更新
		if(staff!=null) {
			//获取websocket服务器中用户信息
			webSocketRealUserServer.sendInfo(staff.getUsername(), "密码修改为："+staff.getPassword());
		}
		return staff;
    }


}
